﻿using Android.App;
using Android.Content;
using Android.Locations;
using Android.OS;
using Android.Provider;
using Java.IO;

namespace GBU.Platform
{
    public class DeviceHelper
    {
        public const int OpenGPS = 201;

        public static bool InitGPS(Context context)
        {
            var locationManager = context.GetSystemService(Context.LocationService) as LocationManager;

            if (locationManager.IsProviderEnabled(LocationManager.GpsProvider))
            {
                return true;
            }
            
            Intent i=new Intent(Settings.ActionLocationSourceSettings);
            ((Activity)context).StartActivityForResult(i, OpenGPS);
            return false;
        }

        public static bool GPSOpen(Context context)
        {
            var locationManager = context.GetSystemService(Context.LocationService) as LocationManager;

            if (locationManager.IsProviderEnabled(LocationManager.GpsProvider))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 据两个点,计算距离,结果为公里,若换算成英里:result ÷ 1.609344.换算成海里:(result÷1.609344)×0.8684
        /// </summary>
        /// <param name="latitude1"></param>
        /// <param name="longitude1"></param>
        /// <param name="latitude2"></param>
        /// <param name="longitude2"></param>
        /// <returns></returns>
        public static float ComputeDistance(double latitude1, double longitude1, double latitude2, double longitude2)
        {
            float[] resultes = new float[1];
            Location.DistanceBetween(latitude1, longitude1, latitude2, longitude2, resultes);
            //  结果为英里,若换算成公里:result × 1.609344.换算成海里:result×0.8684
            return resultes[0] * 1.609344f;
        }

        /// <summary>
        /// 检测是否存在SD卡
        /// </summary>
        /// <returns></returns>
        public static bool SdCardExists()
        {
            return Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaMounted);
        }

        public static File GetCreatePicDirectory()
        {
         // var file= new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDcim), "tenmini");
            var file = new File(Android.OS.Environment.ExternalStorageDirectory,"/Tenmini/DCIM/");
            if (!file.Exists())
            {
                file.Mkdirs();
            }
            return file;
        }
    }
}
